import React, { useState, useEffect } from "react";
import reactLogo from "./assets/react.svg";
import viteLogo from "/vite.svg";
import "./App.css";

function App(props) {
  const { setGlobalState, getGlobalState } = props;
  const [count, setCount] = useState(0);

  useEffect(() => {
    if (getGlobalState) {
      setCount(getGlobalState().count);
    }
  }, [history]);

  const handleClick = () => {
    let newValue = count + 1;
    setCount(newValue);
    setGlobalState?.({ count: newValue });
  };

  return (
    <>
      <div>
        <a href="https://vitejs.dev" target="_blank">
          <img src={viteLogo} className="logo" alt="Vite logo" />
        </a>
        <a href="https://react.dev" target="_blank">
          <img src={reactLogo} className="logo react" alt="React logo" />
        </a>
      </div>
      <h1 style={{ textAlign: "center" }}>{count}</h1>
      <div className="card">
        <button onClick={() => handleClick()}>增加</button>
      </div>
    </>
  );
}

export default App;
